﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using viNews.Classes;
using viNews.Controllers;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine.Pages;

public partial class Admin_Pages_viNews_Categories_CategoryPages_ManageCategoryPages : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        List<Category> categories = new List<Category>();
        List<Category> pagedcategories = new List<Category>();
        CategoryController.GetCategories().ForEach(delegate(Category category)
        {
            if (category.PageId < 1)
                categories.Add(category);
            else
                pagedcategories.Add(category);
        });
        GridViewSimple.DataSource = categories;
        GridViewSimple.DataBind();
        GridViewCATEGORYPAGES.DataSource = pagedcategories;
        GridViewCATEGORYPAGES.DataBind();
    }

    public string GetName(int parentid)
    {
        if (parentid > 0)
        {
            string name = CategoryController.GetCategory(parentid).Name;
            if (string.IsNullOrEmpty(name))
                return null;
            else
                return name + " / ";
        }
        else
            return null;
    }

    public string GetPageName(int categoryid)
    {
        return PageController.GetPage(CategoryController.GetCategory(categoryid).PageId).Name;
    }

    public string GetContentPageName(int categoryid)
    {
        return PageController.GetPage(CategoryController.GetCategory(categoryid).ContentPageId).Name;
    }

    protected void GridViewCATEGORYPAGES_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ConfigurePage")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int categoryid = Convert.ToInt32(((Label)GridViewCATEGORYPAGES.Rows[index].FindControl("LabelCategoryId")).Text);
            Category category = CategoryController.GetCategory(categoryid);
            // change the MODE
            WebModuleController.CURRENT_MODE = WebModuleController.MODES.DESIGNER;
            Response.Redirect("~/Designer.aspx?PageId=" + category.PageId);
        }
        if (e.CommandName == "ConfigureContentPage")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int categoryid = Convert.ToInt32(((Label)GridViewCATEGORYPAGES.Rows[index].FindControl("LabelCategoryId")).Text);
            Category category = CategoryController.GetCategory(categoryid);
            // also change the MODE
            WebModuleController.CURRENT_MODE = WebModuleController.MODES.DESIGNER;
            Response.Redirect("~/Designer.aspx?PageId=" + category.ContentPageId);
        }
        if (e.CommandName == "DeletePages")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int categoryid = Convert.ToInt32(((Label)GridViewCATEGORYPAGES.Rows[index].FindControl("LabelCategoryId")).Text);
            Category category = CategoryController.GetCategory(categoryid);            
            PageInfo page = PageController.GetPage(category.PageId);
            if (page != null)
            {
                PageController.DeletePage(page);
                category.PageId = 0;
            }
            PageInfo contentPage = PageController.GetPage(category.ContentPageId);
            if (contentPage != null)
            {
                PageController.DeletePage(contentPage);
                category.ContentPageId = 0;
            }
            CategoryController.UpdateCategory(category);
            Bind();
        }
    }

    protected void GridViewSimple_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CreateCategoryPage")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int categoryid = Convert.ToInt32(((Label)GridViewSimple.Rows[index].FindControl("LabelCategoryId")).Text);
            Category category = CategoryController.GetCategory(categoryid);
            Category parent = CategoryController.GetParentCategory(categoryid);
            string pagename = "";
            if (parent == null)
                pagename = BaseController.FrontPageName + "_" + category.Name + "_Category";
            else
                pagename = BaseController.FrontPageName + "_" + parent.Name + "/_" + category.Name + "_Category";
            category.PageId = PageController.AddPage(new PageInfo(pagename, "viNews Category Page", PluginController.GetInstalledPlugin("viNews").PluginId));

            string contentpagename = "";
            if (parent == null)
                contentpagename = BaseController.ContentPageName + "_" + category.Name + "_Category";
            else
                contentpagename = BaseController.ContentPageName + "_" + parent.Name + "/_" + category.Name + "_Category";
            category.ContentPageId = PageController.AddPage(new PageInfo(contentpagename, "viNews Category Content Page", PluginController.GetInstalledPlugin("viNews").PluginId));

            CategoryController.UpdateCategory(category);
            Bind();
        }
    }
    protected void ConfigureFrontPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Designer.aspx?PageId=" + BaseController.FrontPageId);
    }
    protected void ConfigureContentPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Designer.aspx?PageId=" + BaseController.ContentPageId);
    }
}
